<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 相对开音节（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>相对开音节构成的结构为&#xff1a;辅音 &#43; 元音&#xff08;aeiou&#xff09;&#43; 辅音(r除外) &#43; e。</p> 
<p>常见的单词有bike、cake等。</p> 
<p>给定一个字符串&#xff0c;以空格为分隔符&#xff0c;反转每个单词中的字母&#xff0c;若单词中包含如数字等其他非字母时不进行反转。</p> 
<p>反转后计算其中含有相对开音节结构的子串个数&#xff08;连续的子串中部分字符可以重复&#xff09;。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>字符串&#xff0c;以空格分割的多个单词&#xff0c;字符串长度&lt;10000&#xff0c;字母只考虑小写</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>含有相对开音节结构的子串个数&#xff0c;注&#xff1a;个数&lt;10000</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:64px;">输入</td><td style="width:434px;">ekam a ekac</td></tr><tr><td style="width:64px;">输出</td><td style="width:434px;">2</td></tr><tr><td style="width:64px;">说明</td><td style="width:434px;">反转后为 make a cake 其中make、cake为相对开音节子串&#xff0c;返回2。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:67px;">输入</td><td style="width:431px;">!ekam a ekekac</td></tr><tr><td style="width:67px;">输出</td><td style="width:431px;">2</td></tr><tr><td style="width:67px;">说明</td><td style="width:431px;">反转后为!ekam a cakeke 因!ekam含非英文字符所以未反转&#xff0c;其中 cake、keke为相对开音节子串&#xff0c;返回2。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题主要是两部分逻辑&#xff1a;</p> 
<ul><li>反转操作&#xff1a;如果单词中包含非小写字母&#xff0c;则不反转&#xff0c;否则反转</li><li>反转操作后&#xff1a;每个单词含有多个相对开音节子串</li></ul> 
<p>其中&#xff0c;反转操作很简单&#xff0c;遍历一遍单词字符&#xff0c;或者用正则表达式 [^a-z] 去匹配单词&#xff0c;看有没有非小写字母&#xff0c;没有的话&#xff0c;就反转单词。</p> 
<p>之后&#xff0c;要求解每个单词中含有多少个相对开音节子串&#xff0c;根据题目对应相对开音节结构定义&#xff0c;这个子串长度固定为4&#xff0c;因此我们可以用滑窗去尺取子串&#xff0c;接下来就是判断子串是否为相对开音节结构&#xff0c;这个最好的方式是基于正则表达式&#xff0c;如下&#xff1a;</p> 
<pre>[^aeiou][aeiou][^aeiour]e</pre> 
<p>一旦子串可以被上面正则匹配成功&#xff0c;则是一个符合要求的相对开音节结构。</p> 
<p></p> 
<p>本题&#xff0c;其实还可以优化逻辑&#xff0c;即将反转单词的动作优化掉。</p> 
<p>假设一个单词不包含非小写字母字符&#xff0c;则理论上该单词要被反转&#xff0c;然后用下面正则匹配滑窗子串&#xff1a;</p> 
<blockquote> 
 <p>[^aeiou][aeiou][^aeiour]e</p> 
</blockquote> 
<p>但是&#xff0c;如果我们不对单词反转呢&#xff1f;那么是不是意味着&#xff0c;只需要将正则反转后&#xff0c;匹配子串即可&#xff1f;反转正则如下&#xff1a;</p> 
<pre>e[^aeiour][aeiou][^aeiou]</pre> 
<hr /> 
<p>2023.07.10</p> 
<p>对于正则表达式&#xff1a;[^aeiou] 来匹配非元音字母 是有缺陷的&#xff0c;因为该正则实际匹配的是 非元音的所有字符&#xff0c;而不是字母。</p> 
<p>但是非元音小写字母的正则写起来实在太繁琐了&#xff0c;因此&#xff0c;我们可以在检测相对开音节之前&#xff0c;对子串做一次是否含有非字母的检查&#xff0c;如果含有非字母&#xff0c;则必然不可能是相对开音节。</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String[] words &#61; sc.nextLine().split(&#34; &#34;);
    System.out.println(getResult(words));
  }

  public static int getResult(String[] words) {
    int count &#61; 0;

    Pattern nonLetter &#61; Pattern.compile(&#34;[^a-z]&#34;);
    Pattern reg1 &#61; Pattern.compile(&#34;[^aeiou][aeiou][^aeiour]e&#34;);
    Pattern reg2 &#61; Pattern.compile(&#34;e[^aeiour][aeiou][^aeiou]&#34;);

    for (String word : words) {
      Pattern reg &#61; nonLetter.matcher(word).find() ? reg1 : reg2;

      for (int i &#61; 0; i &lt;&#61; word.length() - 4; i&#43;&#43;) {
        String seg &#61; word.substring(i, i &#43; 4);

        if (nonLetter.matcher(seg).find()) continue;

        if (reg.matcher(seg).find()) count&#43;&#43;;
      }
    }

    return count;
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const words &#61; line.split(&#34; &#34;);
  console.log(getResult(words));
});

function getResult(words) {
  let count &#61; 0;

  const nonLetter &#61; /[^a-z]/;
  const reg1 &#61; /[^aeiou][aeiou][^aeiour]e/;
  const reg2 &#61; /e[^aeiour][aeiou][^aeiou]/;

  words.forEach((word) &#61;&gt; {
    const reg &#61; nonLetter.test(word) ? reg1 : reg2;

    for (let i &#61; 0; i &lt;&#61; word.length - 4; i&#43;&#43;) {
      const seg &#61; word.substr(i, 4);
      if (nonLetter.test(seg)) continue;
      if (reg.test(seg)) count&#43;&#43;;
    }
  });

  return count;
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
import re

words &#61; input().split()


# 算法入口
def getResult():
    count &#61; 0

    nonLetter &#61; re.compile(r&#34;[^a-z]&#34;)
    reg1 &#61; re.compile(r&#34;[^aeiou][aeiou][^aeiour]e&#34;)
    reg2 &#61; re.compile(r&#34;e[^aeiour][aeiou][^aeiou]&#34;)

    for word in words:
        reg &#61; reg1 if nonLetter.search(word) else reg2

        for i in range(len(word) - 3):
            seg &#61; word[i:i &#43; 4]

            if nonLetter.search(seg):
                continue

            if reg.match(seg):
                count &#43;&#61; 1

    return count


# 算法调用
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>